//
//  NewsListViewController.m
//  Commerce
//
//  Created by xiaofeifei on 15/2/27.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "NewsListViewController.h"
#import "NewsDetailViewController.h"
#import "MapMarketListViewController.h"
#import "NewsCell.h"
#import "MarketCell.h"

@interface NewsListViewController (){
    NSMutableArray *_dataArray;
}

@end

@implementation NewsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc] initWithCapacity:10];
    [self refreshData];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    
    if (_menuID == 13) {
        self.navStr = @"货运市场分布图";
        
        UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 70, self.navView.frame.size.height-38, 60, 30)];
        [rightBtn setTitle:@"地图" forState:UIControlStateNormal];
        [rightBtn setBackgroundImage:[UIImage imageNamed:@"back_n.png"] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(gotoMapList:) forControlEvents:UIControlEventTouchUpInside];
        [self.navView addSubview:rightBtn];
    }
    // Do any additional setup after loading the view from its nib.
}

- (void)gotoMapList:(id)sender{
    MapMarketListViewController *vc = [[MapMarketListViewController alloc] initWithNibName:nil bundle:nil];
    vc.annotationList = _dataArray;
    [self presentViewController:vc animated:YES completion:^{}];
}

- (void)getDataList{
    [self getSpeclalLineList];
}

#pragma mark -请求接口
- (void)getSpeclalLineList{//专线列表
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];
    [parameters addEntriesFromDictionary:@{
                                           @"dong_jing":[NSString stringWithFormat:@"%f",[[GlobalData sharedGlobalData] uselocation].longitude],
                                           @"bei_wei":[NSString stringWithFormat:@"%f",[[GlobalData sharedGlobalData] uselocation].latitude],
                                           @"page_index":[NSString stringWithFormat:@"%d",self.page]}];
    NSString *url;
    if (_menuID == 18) {//新闻
        url = [NSString stringWithFormat:@"%@news/list",URL_ROOT];
    }else if (_menuID == 13) {//货运信息市场
        url = [NSString stringWithFormat:@"%@news/tranlist",URL_ROOT];
    }
    [[BaseNetwork alloc] initWithMethodUrl:url
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  [SVProgressHUD dismiss];
                                  if ([(NSArray *)responseObject count] < self.pageLimit) {
                                      self.canLoadMore = NO;
                                  }else{
                                      self.canLoadMore = YES;
                                  }
                                  if (self.page == 1) {
                                      [self.tableView setContentOffset:CGPointZero animated:NO];
                                      [_dataArray removeAllObjects];
                                      [_dataArray addObjectsFromArray:responseObject];
                                      [self addItemsOnTop];
                                  }else{
                                      [_dataArray addObjectsFromArray:responseObject];
                                      [self addItemsOnBottom];
                                  }
                              }];
    
}

#pragma mark - Table View Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_menuID == 13) {//货运信息市场
        return 70;
    }
    return 60;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_menuID == 13) {//货运信息市场
        static NSString *CellIdentifier = @"Cell";
        
        MarketCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MarketCell" owner:self options:nil];
            cell = (MarketCell *)[array objectAtIndex:0];
        }
        [cell configUIWithData:_dataArray[indexPath.row]];
        
        return cell;

    }
    static NSString *CellIdentifier = @"Cell";
    
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"NewsCell" owner:self options:nil];
        cell = (NewsCell *)[array objectAtIndex:0];
    }
    [cell configUIWithData:_dataArray[indexPath.row]];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NewsDetailViewController *vc = [[NewsDetailViewController alloc] initWithNibName:nil bundle:nil];
    vc.newsInfo = _dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
